import 'package:flutter/material.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/book/BookItem.dart';
import 'package:ichinesereader/utils/log.dart';

import '../../models/assessment/questionRecordInfoResult.dart';
import '../../models/assessment/questionRecordSection.dart';
import '../common/bookQuestionCollapse.dart';

class BookQuickReview extends StatefulWidget {
  BookQuickReview(
      {Key? key,
      required this.bookItem,
      this.readingType,
      this.questionRecordId,
      this.submited})
      : super(key: key);
  final BookItem bookItem;
  final NanhaiBookReadingTypes? readingType;
  final bool? submited;
  final String? questionRecordId;
  static const String routeName = '/book/quickreview';

  @override
  State<BookQuickReview> createState() => _BookQuickReviewState();
}

class _BookQuickReviewState extends State<BookQuickReview> {
  final AssessmentsController _assessmentsController = Get.find();
  List<QuestionRecordSection> bookQuickReviewSections = [];
  var bookItem;
  var recordStatus = 'UNDONE';
  @override
  void initState() {
    QuestionRecordInfo? result = _assessmentsController.questionRecordInfo;
    if (result != null) {
      if (result.recordStatus != null) {
        recordStatus = result.recordStatus!;
      }
      //call it too later in here, so move to book widget
      // if (recordStatus == 'COMPLETED' && widget.questionRecordId != null) {
      //   _assessmentsController
      //       .fetchAssessmentRecordGrading(widget.questionRecordId!);
      // }
      setState(() {
        bookQuickReviewSections =
            result.list!.where((i) => i.questions?.length != 0).toList();
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build book quick review');
    return Scaffold(
      appBar: AppBar(
        title: Container(
          width: double.infinity,
          height: 40,
          child: Center(
            child: Text(this.widget.bookItem.bookTitle.toString()),
          ),
        ),
      ),
      body: bookQuickReviewSections.length > 0
          ? BookQuestionCollapse(
              bookcode: '${widget.bookItem.bookCode}-review',
              recordStatus: recordStatus,
              bookItem: widget.bookItem,
              readingType: widget.readingType,
              nanhaiBookKnowledgeTypes: NanhaiBookKnowledgeTypes.quickReview,
              bookQuestionCollapseSections: bookQuickReviewSections,
              kind: "TEST",
              submited: widget.submited)
          : Text("Book quick review data coming soon".tr),
    );
  }
}
